/*
 * Calendar.c
 *
 * Created: 2012-07-09 16:31:28
 *  Author: Sebastian_2
 */ 

#include "Menus.h"

void Menu_Calendar_Enter() {
	mCalendarMonthToDisplay = gRTC.month;
	Menu_Calendar_Refresh();
}

void Menu_Calendar_Refresh() {
	gMenu = MENU_CALENDAR;
	// calculate first day of the month using Zeller's formula
	uint8_t month;
	uint16_t year = gRTC.year;
	if(mCalendarMonthToDisplay >= 3) {
		month = mCalendarMonthToDisplay;
	} else {
		month = mCalendarMonthToDisplay + 12;
		year--;
	}
	
	uint8_t k = year%100;
	uint8_t j = year/100;
	volatile uint16_t day = 1 + 13*(month+1)/5;
	day += k + (k>>2);
	day += (j>>2) + 5*j;
	day %= 7;
	// 0 = Saturday, 1 = Sunday, 2 = Monday, ... - need to change that
	if(day == 0 || day == 1)
	day += 6;
	else
	day--;
	
	// calculate amount of days in the month
	uint8_t days = 0;
	uint16_t year_full = gRTC.year + 2000;
	uint8_t leapYear;
	if(year_full%400 == 0 || year_full%4 == 0)
	leapYear = 1;
	else
	leapYear = 0;
	if(mCalendarMonthToDisplay == 1 || mCalendarMonthToDisplay == 3 || mCalendarMonthToDisplay == 5 || mCalendarMonthToDisplay == 7 || mCalendarMonthToDisplay == 8 || mCalendarMonthToDisplay == 10 || mCalendarMonthToDisplay == 12)
	days = 31;
	else if(mCalendarMonthToDisplay == 4 || mCalendarMonthToDisplay == 6 || mCalendarMonthToDisplay == 9 || mCalendarMonthToDisplay == 11)
	days = 30;
	else if(mCalendarMonthToDisplay == 2 && leapYear == 1)
	days = 29;
	else if(mCalendarMonthToDisplay == 2 && leapYear == 0)
	days = 28;
	// now print entire month on the screen
	char month_str[20];
	switch(mCalendarMonthToDisplay) {
		case 1:
		sprintf(month_str, "Styczen  %u", year_full);
		break;
		
		case 2:
		sprintf(month_str, "Luty  %u", year_full);
		break;
		
		case 3:
		sprintf(month_str, "Marzec  %u", year_full);
		break;
		
		case 4:
		sprintf(month_str, "Kwiecien  %u", year_full);
		break;
		
		case 5:
		sprintf(month_str, "Maj  %u", year_full);
		break;
		
		case 6:
		sprintf(month_str, "Czerwiec  %u", year_full);
		break;
		
		case 7:
		sprintf(month_str, "Lipiec  %u", year_full);
		break;
		
		case 8:
		sprintf(month_str, "Sierpien  %u", year_full);
		break;
		
		case 9:
		sprintf(month_str, "Wrzesien  %u", year_full);
		break;
		
		case 10:
		sprintf(month_str, "Pazdziernik  %u", year_full);
		break;
		
		case 11:
		sprintf(month_str, "Listopad  %u", year_full);
		break;
		
		case 12:
		sprintf(month_str, "Grudzien  %u", year_full);
		break;
	}
	uint16_t len = TextLength(month_str, &tahoma12ptFontInfo);
	point p = {(SSD1289_MaxX-len)>>1, 2};
	SSD1289_Clear();
	SSD1289_PrintString(month_str, &p, &tahoma12ptFontInfo);
	p.y = 24;

	uint8_t column = day-1;
	for(uint8_t i = 0; i < days; i++) {
		char tmp[3];
		point p2;
		sprintf(tmp, "%d", i+1);
		uint16_t len = TextLength(tmp, &centuryGothic20ptFontInfo);
		p.x = 2 + column*47;
		p.x += (36-len) >> 1; // (x >> 1) == x/2
		if(i+1 == gRTC.date && mCalendarMonthToDisplay == gRTC.month) {
			gColour = CALENDAR_FONT_COLOUR;
			p2 = SSD1289_PrintString(tmp, &p, &centuryGothic20ptFontInfo);
			gColour = FONT_COLOUR;
		} else if(p.x >= 210) { // weekend
			gColour = CALENDAR_WEEKEND;
			p2 = SSD1289_PrintString(tmp, &p, &centuryGothic20ptFontInfo);
			gColour = FONT_COLOUR;
		} else {
			p2 = SSD1289_PrintString(tmp, &p, &centuryGothic20ptFontInfo);
		}
		if(p2.x >= 280) {
			p = IncY(&p, 38);
			column = 0;
		} else {
			if(column <= 6) {
				column++;
			} else {
				column = 0;
			}
		}
	}
	// draw separators
	// horizontal goes first
	p = (point){0, 50};
	for(uint8_t i = 0; i < 5; i++) {
		drawXSeparator(&p, SSD1289_MaxX, SEPARATOR3);
		p = IncY(&p, 39);
	}
	// and now vertical
	p = (point){45, 24};
	for(uint8_t i = 0; i < 6; i++) {
		drawYSeparator(&p, SSD1289_MaxY, SEPARATOR3);
		p = IncX(&p, 47);
	}
}